//
//  AppDelegate.m
//  HYApp
//
//  Created by Sunline on 2018/11/14.
//  Copyright © 2018 Unidax. All rights reserved.
//

#import "AppDelegate.h"
#import "ViewController.h"
#import "HYAppMsgModuleManager.h"


#import "HYFunctionManager.h"
#import "HYLaunchViewController.h"

#import "LoginViewController.h"
#import "CustomNavigationController.h"
#import "HomePageViewController.h"
#import "HYApp-Swift.h"
#import "LoginViewController.h"
#import "JFGuidePagesViewController.h"
#import "ViewController.h"

#define k_JFApp_FirstLaunch @"k_AW_JFApp_FirstLaunch"

@interface AppDelegate ()
//控制手势 密码的
@property (nonatomic) BOOL isEnterBackgoud;  //是否进入后台了
@property (nonatomic) NSDate * backgroudDate;  //进入后台时期
@property (nonatomic) NSDate * activeDate;  //进入前台时期


@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [JFEnvironmentManager shareInstance].isDebug = YES;
    
    [JFMsgCenteManager registerModule:[HYAppMsgModuleManager sharedInstance] moduleName:@"App"];
    [HYLanguageService initHYLanguageShowType]; //初使化语言
    

    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(setRootVCIsHomePage) name:@"changeLanguage" object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(setRootVCIsHomePage) name:@"changeRoot" object:nil];
    
    CGRect rect = [UIScreen mainScreen].bounds;
    self.window = [[UIWindow alloc] initWithFrame:rect];
    self.window.backgroundColor = [UIColor whiteColor];
    if ([[NSUserDefaults standardUserDefaults] boolForKey:k_JFApp_FirstLaunch]) {//如果不是第一次安装就不需要去初始化
          self.window.rootViewController = [ViewController new];
         [self.window makeKeyAndVisible];
    }else{
        JFGuidePagesViewController * vc = [[JFGuidePagesViewController alloc] init];
        WEAKSELF
        vc.completeBlock = ^{
            weakSelf.window.rootViewController = [ViewController new];
            [weakSelf.window makeKeyAndVisible];
        };
        self.window.rootViewController = vc;
        [self.window makeKeyAndVisible];
    }
    self.isEnterBackgoud = YES;
    return YES;
}
- (void)setLoginVCIsHomePage {
    LoginViewController *vc1 = [[UIStoryboard storyboardWithName:@"Login" bundle:nil]instantiateViewControllerWithIdentifier:@"LoginViewController"];
    CustomNavigationController *vc = [[CustomNavigationController alloc]initWithRootViewController:vc1];
    self.window.rootViewController = vc;
    [self.window makeKeyAndVisible];
}

- (void)setRootVCIsHomePage {
    HomePageViewController *vc1 = [[HomePageViewController alloc]init];
    CustomNavigationController *vc = [[CustomNavigationController alloc]initWithRootViewController:vc1];
    self.window.rootViewController = vc;
    [self.window makeKeyAndVisible];
}
//-(void)jiexiwenian{
    //    NSDictionary * dict = (NSDictionary*)[self getJsonDataJsonname:@"ZH-TW"];
    //      NSString * tempStr = @"";
    //      for (NSString * key in dict.allKeys) {
    //          id vaule = dict[key];
    //          if ([vaule isKindOfClass:[NSDictionary class]]) {
    //              NSDictionary * tempdict = (NSDictionary*)vaule;
    //              for (NSString * tempkey in tempdict.allKeys) {
    //                   tempStr = [NSString stringWithFormat:@"%@\n\"%@\"=\"%@\";",tempStr,[NSString stringWithFormat:@"%@.%@",key,tempkey],tempdict[tempkey]];
    //              }
    //          }else{
    //              tempStr = [NSString stringWithFormat:@"%@\n\"%@\"=\"%@\";",tempStr,key,vaule];
    //          }
    //      }
      
    //    [JFInternationalHelper changeLanguage:@"en"];
    //    NSString * str = JFMainLocalizedString(@"myProfile.please-enter-phone");
//}

//- (id)getJsonDataJsonname:(NSString *)jsonname
//{
//    NSString *path = [[NSBundle mainBundle] pathForResource:jsonname ofType:@"json"];
//    NSData *jsonData = [[NSData alloc] initWithContentsOfFile:path];
//    NSError *error;
//    id jsonObj = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
//    if (!jsonData || error) {
//        return nil;
//    } else {
//        return jsonObj;
//    }
//}


- (void)applicationWillResignActive:(UIApplication *)application {
     self.backgroudDate = [NSDate date];
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
     self.isEnterBackgoud = YES;
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    NSDate * activeDate = [NSDate date];
    NSDate * backgroundDate =  self.backgroudDate ;
    if (backgroundDate == nil) {
        backgroundDate = [NSDate date];
    }
    
    NSTimeInterval b = [activeDate timeIntervalSince1970] - [backgroundDate timeIntervalSince1970];
    BOOL isShow = NO;
    if(b<0)
    {
        isShow = NO;
    }else{
        if(b>59) //1分钟
        {
            isShow = YES;
        }
    }
    //进入后台5分钟 要验证手势或指纹
    if( self.isEnterBackgoud == YES && isShow ) //登录过 从后台 打开了手势
    {
        
        //去user组件中
        [HYFunctionManager handleGesturePring];
  
    }

    
    
    self.isEnterBackgoud = NO;
    
}


- (void)applicationWillTerminate:(UIApplication *)application {
}
-(BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options{
    return [A0WebAuth resumeAuthWithURL:url options:options];
}

@end
